package com.lzjt.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzjt.entity.MedicalPolicyEntity;
import com.lzjt.mapper.MedicalPolicyMapper;
import com.lzjt.model.MedicalPolicyModel;
import com.lzjt.param.MedicalPolicyParam;
import com.lzjt.util.Msg;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service

public class MedicalPolicyService {
    @Autowired
    private MedicalPolicyMapper medicalPolicyMapper;
    public Msg saveMedicalPolicy(MedicalPolicyParam param){
        param.setCreateTime(new DateTime().toString("yyyy-MM-dd"));
        MedicalPolicyEntity mpEntity=new MedicalPolicyEntity();
        BeanUtils.copyProperties(param,mpEntity);
        int i=medicalPolicyMapper.saveMedialPolicy(mpEntity);
        List<MedicalPolicyModel> allPolicy=medicalPolicyMapper.getAllPolicy(null);
        mpEntity.setTotal((long) allPolicy.size());
        if(i>0){
            Long num=mpEntity.getTotal()%5==0?(mpEntity.getTotal()/5):(mpEntity.getTotal()/5)+1;
            return Msg.success().mess("添加成功").data("numberOfAdd",i).data("pages",num);
        }
        return Msg.fail().mess("添加失败");
    }
    public Msg getMedicalPolicyWithPage(MedicalPolicyParam param){
        // 使用正确的参数获取方法，例如 getPageSize()
        PageHelper.startPage(param.getPn(), param.getPageSize());

        List<MedicalPolicyModel> list = medicalPolicyMapper.getAllPolicy(param);
        PageInfo<MedicalPolicyModel> info = new PageInfo<>(list, 5);

        // 使用 data() 方法替代 add()
        return Msg.success().data("pageInfo", info);
    }

    public Msg updateMedicalPolicy(Long id,MedicalPolicyParam param){
        MedicalPolicyEntity mpEntity=new MedicalPolicyEntity();
        BeanUtil.copyProperties(param,mpEntity);
        mpEntity.setId(id);
        int i=medicalPolicyMapper.updateMedialPolicy(mpEntity);
        if(i>0){
            return Msg.success().mess("修改成功").data("updateData",mpEntity);
        }
        return Msg.fail().mess("修改失败");
    }
    public Msg deleteMedicalPolicy(Long id){
        int i=medicalPolicyMapper.deleteMedialPolicy(id);
        if(i>0){
            return Msg.success().mess("删除成功").data("numberOfMPolicyDelete",i);
        }
        return Msg.fail().mess("删除失败");
    }
}
